package com.litchi.rest;

import com.litchi.entity.Train;
import com.litchi.persistence.repo.TrainRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @author palad
 * 2016年4月29日
 */

@RestController
@RequestMapping("/api/trains")
public class TrainController {
	@Autowired
	TrainRepository trainRepository;

    @RequestMapping("hello")
    public @ResponseBody String hello() {
        return "Hello in trains!";
    }

	@RequestMapping("")
    public @ResponseBody Iterable<Train> getTrainList() {
        return trainRepository.findAll();
    }

    @RequestMapping(value = "/addTrain", method = RequestMethod.POST)
    public @ResponseBody void addTrain(@RequestBody Train train) {
        trainRepository.save(train);
    }

    @RequestMapping(value = "/updateTrain", method = RequestMethod.PUT)
    public @ResponseBody void updateTrain(@RequestBody Train train) {
    	trainRepository.save(train);
    }

    @RequestMapping(value = "/removeTrain/{id}", method = RequestMethod.DELETE)
    public @ResponseBody void removeTrain(@PathVariable("id") Long id) {
        trainRepository.delete(id);
    }

    @RequestMapping(value = "/removeAllTrains", method = RequestMethod.DELETE)
    public @ResponseBody void removeAllTrains() {
        trainRepository.deleteAll();
    }


}
